// 问卷保存和提交

export {bridge}

let bridge = []; 

// 保存问卷
$('.question-preservation').click(() => {
    get_all("未发布");
});

// 点击发布问卷按钮
$('.question-commit').click(() => {
    let str = $('#time').val();
    if (str === "") {
        alert("您未选择截止时间!");
        return;
    } else if (new Date().valueOf() > new Date(str).valueOf()) {
        alert("您选择的截止日期已过!");
        return;
    }
    $('.tips').show();
    $('.tips').css("background", "rgb(153, 153, 153, 0.5)");
    $('.new-questionnaire-content').css("background", "rgb(153, 153, 153)");
    document.getElementById('tips-content-p').innerHTML = "(此问卷的截止日期为" + str + ")";

    $('.determine').click((e) => {
        get_all("发布中");
    })
});

// 获取问卷的所有信息
let get_all = (type) => {
    bridge.splice(0);
    let title = $('.editing-questionnaire-title>.color').text();
    bridge.push(title);
    let $first = $('.option').children().eq(0);
    while ($first.length !== 0) {
        // 获取问题
        let t = [];
        t.push($first.find('p').text());
        let $first_child = $first.find('.question').children().eq(0);
        while ($first_child.length !== 0) {
            t.push($first_child.text());
            $first_child = $first_child.next();
        }
        $first = $first.next();
        bridge.push(t);
    }

    console.log($('.option>.Q1>.t1 .option-text-s').attr('placeholder'));

    bridge.push(get_time());
    bridge.push(type)
    console.log(bridge)

    // 点击保存之后出现新界面
    window.close()
    window.open("manage.html");
}

// 获取当前时间 格式为  2023-1-5 19:38:29
let get_time = () => {
    let myDate = new Date;
    let year = myDate.getFullYear(); //获取当前年
    let mon = myDate.getMonth() + 1; //获取当前月
    let date = myDate.getDate(); //获取当前日
    let h = myDate.getHours();//获取当前小时数(0-23)
    let m = myDate.getMinutes();//获取当前分钟数(0-59)
    let s = myDate.getSeconds();//获取当前秒
    return `${year}-${mon}-${date} ${h}:${m}:${s}`;
}

$('.tips-head>div>i').click(() => {
    $('.tips').hide();
});

$('.tips-content>div>.cancel').click(() => {
    $('.tips').hide();
});

$('.tips-content>div>.determine').click(() => {
    $('.tips').hide();
});